
layui.define(['form','xmSelect','common'], function(exports){
	
	var $ = layui.$
	,setter = layui.setter
	,xmSelect = layui.xmSelect
	,common = layui.common
	,form = layui.form;
	//取出当前点击的menuId
	var parentJson = {};
   
   	$(window).resize(function() {
	   	var bodyC = $('.layui-card-body').css("height");
	   	var zhi = layui.common.PxConvertToNum(bodyC);
		$('.layui-own-border').css("height", zhi-90);
   	});
	var groupIdTreeSelect = xmSelect.render({
		el: '#groupIdTreeSelect',
		autoRow: true,
		name: 'groupId',
		toolbar:{
			show: true,
			list: [ 'ALL', 'CLEAR', 'REVERSE' ]
		},
		tree: {
			strict:false,
			show: true,
			showFolderIcon: true,
			showLine: true,
			indent: 20,
			expandedKeys: [ -1 ],
		},
		height: 'auto',
		data:[]
	});
	
   	var obj = {
		setParentJson: function setParentJson(json){
			parentJson = json;
			if(parentJson.modifyType == 'add'){
				$('select[name="company"]').val('shoudongtianjia')
			}
			load();
	   } 
   }
   	form.verify({
		name: function(value){
			if(value.length < 1){
				return '昵称至少得1个字符啊';
			}
		}
		,pass: [/(.+){6,12}$/, '密码必须6到12位']
		,content: function(value){
			layedit.sync(editIndex);
		}
		,command: [/(.+){3,12}$/, '口令必须3位以上']
		,content: function(value){
			layedit.sync(editIndex);
		}
	});
   function load(){
	   $.ajax({
			url: layui.setter.host+'/adminmanager/group/getTreeSelect'
			,type:'post'
			,contentType: "application/json; charset=utf-8"
			,dataType:'json'
			,success: function(res){
				if(res.status == setter.response.statusCode.status_Success){
					groupIdTreeSelect.update({
						data: res.data,
						autoRow: true,
					})
					groupIdTreeSelect.setValue(parentJson.groupId.split(','));
				}else{
					layer.msg(res.msg);
				}
			}
		});   
	   	form.val('form1',{
			"userId":parentJson.userId
			,"username":parentJson.username
//			,"password":parentJson.password
			,"command":parentJson.command
			,"name":parentJson.name
			,"phone":parentJson.phone
			,"mail":parentJson.mail
			,"sex":parentJson.sex
			,"mainPage":parentJson.mainPage
			,"status":parentJson.status
			,"jobnumber":parentJson.jobnumber
			,"remark":parentJson.remark
			,"remark":parentJson.company
			}
		);
				
	  form.render();
		$('.layui-btn').on('click', function(){
			var type = $(this).data('type');
			active[type] ? active[type].call(this) : '';
		});
	   
		var active = {
			back: function(){
				backUrl();
			}
			,submit:function(){
				form.on('submit(save)', function(data){
					var field = data.field;
					var modifyType = parentJson.modifyType;
					var modifyUrL = '';
					if(modifyType=='add'){
						modifyUrL = layui.setter.host+'/adminmanager/user/add';
					}else{
						modifyUrL = layui.setter.host+'/adminmanager/user/edit';
						field.userId = parentJson.userId;
					}
				
				//	console.log(data.field.command.length);
				//	if(data.field.command.length>=3){
						layui.admin.req({
							url: modifyUrL
							,type:'post'
							,dataType:'json'
							,contentType: "application/json; charset=utf-8"
							,data:JSON.stringify(field) 
							,done: function(res){
							//请求成功后
								if(res.status == setter.response.statusCode.status_Success){
									layer.msg(res.msg,{time:1000},function(){
										backUrl();
									});
								}else{
									layer.msg(res.msg);
								}
							}
						});
			//	    }else{return layer.msg('请填写3位以上口令');}
				});
				$('[lay-filter="save"]').trigger('click');
			}
			,reset:function(){
				$('[lay-filter="form1"]')[0].reset();
				form.render();
			}
		};
		function backUrl(){
			var bakUrl = parentJson.bakUrl;
			window.location.href = bakUrl;
		} 
   }
   
	exports('edit', obj)
});